package com.seelyn.ddvideo.weixin.handler;

import com.github.sd4324530.fastweixin.handle.MessageHandle;
import com.github.sd4324530.fastweixin.message.BaseMsg;
import com.github.sd4324530.fastweixin.message.NewsMsg;
import com.github.sd4324530.fastweixin.message.TextMsg;
import com.github.sd4324530.fastweixin.message.req.TextReqMsg;
import com.seelyn.ddvideo.model.Video;
import com.seelyn.ddvideo.service.VideoService;
import com.seelyn.ddvideo.spider.MovieSubTypeEnum;
import com.seelyn.ddvideo.spider.MovieTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @author ifcousing-linfeng
 * @since 2017/6/20
 */
@Component
public class SearchVideoMessageHandle implements MessageHandle<TextReqMsg> {

    private Logger LOG = LoggerFactory.getLogger(SearchVideoMessageHandle.class);
    @Autowired
    private VideoService videoService;

    @Value("${site.domain:'http://www.laikan.site'}")
    private String siteDomain;

    @Override
    public BaseMsg handle(TextReqMsg message) {

        Pageable pageable = new PageRequest(0, 8, Sort.Direction.DESC, "onlineTime");
        Page<Video> videoPage = null;
        String title = message.getContent();
        LOG.info("微信接收消息：" + title);

        if (title.startsWith("login")) {



        } else if (title.contains("@")) {

        } else {
            if ("今日更新".equals(title) || "最新".equals(title)) {
                videoPage = videoService.findVideoByMovieTypeLessThanEqual(MovieTypeEnum.all.ordinal(), pageable);
            } else if ("最新电视剧".equals(title)) {
                videoPage = videoService.findVideoByMovieType(MovieTypeEnum.tv.ordinal(), pageable);
            } else if ("最新电影".equals(title)) {
                LOG.info("微信接收消息1：" + title);
                videoPage = videoService.findVideoByMovieType(MovieTypeEnum.movie.ordinal(), pageable);
                LOG.info("微信接收消息2：" + title);
            } else if ("最新动漫".equals(title)) {
                videoPage = videoService.findVideoByMovieType(MovieTypeEnum.cartoon.ordinal(), pageable);
            } else if ("最新伦理".equals(title) || "最新伦理片".equals(title)) {
                videoPage = videoService.findVideoByTitleLikeAndMovieTypeLessThanEqualAndSubType(title, MovieTypeEnum.all.ordinal(), MovieSubTypeEnum.LUNLIPIAN.ordinal(), pageable);
            } else {
                videoPage = videoService.findVideoByTitleLikeAndMovieTypeLessThanEqual(title, MovieTypeEnum.all.ordinal(), pageable);
            }
        }

        if (videoPage == null || videoPage.getContent().isEmpty()) {
            return new TextMsg("影片没有找到!俺已经记录下来了，会通知主人的。");
        }
        NewsMsg newsMsg = new NewsMsg((int) videoPage.getTotalElements());
        for (Video video : videoPage.getContent()) {
            newsMsg.add(video.getTitle(), video.getPicUrl(), "http://www.laikan.site/detail/" + video.getId());
        }
        if (newsMsg.getArticles().isEmpty()) {
            return new TextMsg("影片没有找到!俺已经记录下来了，会通知主人的。");
        }
        return newsMsg;

    }

    @Override
    public boolean beforeHandle(TextReqMsg message) {

        return !StringUtils.isEmpty(message.getContent());

    }

}
